<template>
  <div>
    <el-dialog
      title="分配角色"
      :visible="dialogVisible"
      width="70%"
      @close="close"
    >=={{ roleIds }}
      <el-checkbox-group v-model="roleIds">
        <el-checkbox
          v-for="(item, index) in roleList"
          :key="index"
          :label="item.id"
        >{{ item.name }}</el-checkbox>
      </el-checkbox-group>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="userAddRole">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserInfo } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: '',
  props: ['id', 'dialogVisible'],
  data() {
    return {
      roleIds: [], //  当前拥有的角色
      roleList: [] //  所有的角色
    }
  },
  watch: {
    id: {
      handler(newVal) {
        if (newVal) {
          this.getRole()
        }
      },
      immediate: true
    }
  },
  methods: {
    async getRole() {
      const res = await getRoleList()
      console.log(res, 887)
      this.roleList = res.rows

      const res2 = await getUserInfo(this.id) //  获取当前用户的信息
      this.roleIds = res2.roleIds
    },
    close() {
      this.$emit('update:dialogVisible', false)
    },
    async userAddRole() {
      //  用户添加角色
      await assignRoles({
        id: this.id,
        roleIds: this.roleIds
      })

      this.close()
    }
  }
}
</script>
<style lang="less" scoped></style>
